package com.bjnet.castdemo.bjcast.imp;

import android.util.Log;

import com.bjnet.castdemo.CastManager;
import com.bjnet.castdemo.DemoApplication;
import com.bjnet.castdemo.R;
import com.bjnet.castdemo.base.MediaChannelCtx;
import com.bjnet.castdemo.event.BJCastEvent;
import com.bjnet.castdemo.event.BJCastSurfaceEvent;
import com.bjnet.castdemo.event.CloseChannelEvent;
import com.bjnet.castdemo.event.CreateChannelFailedEvent;
import com.bjnet.cbox.module.MediaChannel;
import com.bjnet.cbox.module.MediaChannelInfo;
import com.bjnet.cbox.module.ModuleImpItf;
import com.bjnet.cbox.module.UserInfo;
import com.bjnet.cbox.module.Util;

public class BJCastModuleImp extends ModuleImpItf {

    boolean channelAvailable(){
        return CastManager.getMgr().isChannelAvailable();
    }

    protected boolean openAndStartChannel(MediaChannel channel){
        boolean ret = false;
        CastManager.getMgr().registerChannel(channel);
        if (channel.open()) {
            //channel.start(userInfo);
            Log.i(DemoApplication.TAG, " reqMediaChannel open success id:"+channel.getChannelId());
            ret = true;
        } else {
            channel.close();
            CastManager.getMgr().unregisterChannel(channel);
            Log.i(DemoApplication.TAG, " reqMediaChannel open failed");
        }
        return ret;
    }

    @Override
    public MediaChannel reqMediaChannel(MediaChannelInfo channelInfo, UserInfo userInfo) {
        if(!channelAvailable()){
            Log.e(DemoApplication.TAG, "reqMediaChannel channelAvailable false");
            CreateChannelFailedEvent event = new CreateChannelFailedEvent(userInfo,DemoApplication.getContext().getResources().getString(R.string.err_screen_full));
            DemoApplication.APP.getEventBus().post(event);
            return null;
        }
        Log.i(DemoApplication.TAG, "reqMediaChannel userInfo:"+userInfo.toString());
        android.app.Activity activity = com.blankj.utilcode.util.ActivityUtils.getTopActivity();
        if(!(activity instanceof com.bjnet.castdemo.activity.LoginActivity)){
            Log.e(DemoApplication.TAG, "reqMediaChannel failed. not in home activity :"+activity);
            CreateChannelFailedEvent event = new CreateChannelFailedEvent(userInfo,DemoApplication.getContext().getResources().getString(R.string.err_cannot_share_notin_home));
            DemoApplication.APP.getEventBus().post(event);
            return null;
        }

        GLScreenRenderChannel channel = new GLScreenRenderChannel(channelInfo);
        if (!openAndStartChannel(channel)){
            Log.e(DemoApplication.TAG, "reqMediaChannel failed,because openAndStartChannel failed.");
            return null;
        }

        if (UserInfo.CALL_DEVICE_IOS_APP == userInfo.deviceType){
            byte[]  airplay_eld_buffer = new byte[4];
            airplay_eld_buffer[0] = (byte) 0xF8;
            airplay_eld_buffer[1] = (byte) 0xE8;
            airplay_eld_buffer[2] = (byte) 0x50;
            airplay_eld_buffer[3] = (byte) 0x00;
            channel.setAudioTrackCodecInfo(airplay_eld_buffer, "csd-0");
        }else{
            byte[]  buffer = new byte[2];
            buffer[0] = (byte) 0xb9;
            buffer[1] = (byte) 0x91;
            channel.setAudioTrackCodecInfo(buffer, "csd-0");
        }

        if (Util.getApiLevel() < 19){
            DemoApplication.APP.getEventBus().post(new BJCastSurfaceEvent(channel));
        }else{
            if (UserInfo.CALL_DEVICE_IOS_APP == userInfo.deviceType){
                DemoApplication.APP.getEventBus().post(new BJCastEvent(channel));
            }else{
                DemoApplication.APP.getEventBus().post(new BJCastSurfaceEvent(channel));
            }
        }

        return channel;
    }

    @Override
    public void relMediaChannel(MediaChannel channel) {
        channel.close();
        CloseChannelEvent event = new CloseChannelEvent(channel.getChannelId());
        DemoApplication.APP.getEventBus().post(event);
        CastManager.getMgr().updateChannelFlag(channel.getChannelId(), MediaChannelCtx.CHANNEL_CLOSED_MASK);
    }
}
